package com.wrupple.muba.catalogs.client.activity.impl;

import java.util.Arrays;

import com.google.gwt.user.client.ui.Image;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.activity.CatalogEntryImportActivity;
import com.wrupple.muba.catalogs.client.activity.process.impl.EditionActivityProcessImpl.ExitToBrowseActivity;
import com.wrupple.muba.catalogs.client.activity.process.impl.ImportDataHandler;
import com.wrupple.muba.catalogs.client.activity.process.impl.SimpleImportProcess;
import com.wrupple.muba.catalogs.client.activity.process.state.BrowsePlaceInterpret;
import com.wrupple.muba.catalogs.client.activity.process.state.SimpleImportDataHandler;
import com.wrupple.muba.catalogs.client.activity.process.task.impl.CSVImportTask;
import com.wrupple.muba.catalogs.client.module.services.logic.CSVParser;
import com.wrupple.muba.catalogs.client.module.services.presentation.CatalogPlaceInterpret;
import com.wrupple.muba.catalogs.client.view.ImportView;
import com.wrupple.muba.catalogs.client.widgets.impl.CatalogTheme;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.client.activity.AbstractSequentialActivity;
import com.wrupple.muba.common.client.activity.ActivityProcess;
import com.wrupple.muba.common.client.activity.ActivityProcessImpl;
import com.wrupple.muba.common.client.application.DataCallback;
import com.wrupple.muba.common.domain.DesktopPlace;
import com.wrupple.muba.common.shared.State.DesktopManager;
import com.wrupple.muba.common.shared.State.ProcessManager;

public class GenericImportActiviy extends AbstractSequentialActivity<ProcessManager> implements CatalogEntryImportActivity {

	private BrowsePlaceInterpret bpi;
	private CatalogPlaceInterpret interpret;
	private CSVParser parser;
	private ImportView view;
	private CatalogTheme theme;

	@Inject
	public GenericImportActiviy(CatalogTheme theme,ProcessManager pm,
			DesktopManager dm,CatalogPlaceInterpret interpret,BrowsePlaceInterpret bpi, CSVParser p, ImportView v) {
		super(pm, dm);
		this.bpi=bpi;
		this.theme=theme;
		this.interpret=interpret;
		this.parser=p;
		this.view=v;
		bpi.setPlaceInterpret(interpret);
	}

	@Override
	public void getActivityProcess(DesktopPlace item,
			DataCallback<ActivityProcess> callback) {
		ActivityProcess result=new ActivityProcessImpl();
		ExitToBrowseActivity exit = new ExitToBrowseActivity();
		CSVImportTask readData= new CSVImportTask(view, parser);
		ImportDataHandler strategyToPersistAllData= new SimpleImportDataHandler();
		SimpleImportProcess<CatalogEntry> process = new SimpleImportProcess<CatalogEntry>(interpret, bpi, readData, strategyToPersistAllData);
		Image ok = new Image(theme.ok());
		readData.setAction(Arrays.asList(ok));
		
		exit.setCatalogIdProvider(bpi.getCatalogIdProvider());
		result.addAll(process);
		result.addState(exit);
		callback.setResultAndFinish(result);
	}



}
